
import re
from crawler.htmlparser import replace_nbsp, remove_html_tags
from urllib import urlopen
import urllib2
__author__="tslawek"
__date__ ="$2008-11-21 18:38:05$"

if __name__ == "__main__":
    print "Hello";


class Onet:

    def __init__(self, address):

        self.address = address
        start_base = '.blog.onet.pl/'
        self.base = self.address[0:self.address.find(start_base)]+start_base
        

    def start(self):
        response = urllib2.urlopen(self.address)
        self.html = response.read()

    def get_html(self, link):
        response = urllib2.urlopen(link)
        return response.read()

    def get_blog_by_address(self):
        pat = re.compile("<DIV class=bl_body>(.*?)</DIV>")
        
        
        s = pat.search(str(self.html))
        t = s.group(1)

        return replace_nbsp(remove_html_tags(t)).decode('iso-8859-2')

    def get_other_blogs_address(self):

        pat = re.compile("<td class=\"wl_te internal_tekst\"(.*?)</td>")
        #response = urllib2.urlopen(self.address)
        #html = response.read()
        s = pat.search(str(self.html))
        try:
          options = s.group(1)
        except:
          options = "NIC NIE MA"
        pat = re.compile(r'<OPTION(.*?)</OPTION>')
        options_list = pat.findall(options)


        pat2 = re.compile('value=\"(.*?)\">(.*?)\((.*?); (.*?)\)')
        info = []
        for blog in options_list:

            tmp  = pat2.search(blog)
            try:
                info.append( (tmp.group(1).strip().decode('iso-8859-2'), tmp.group(2).strip().decode('iso-8859-2'), tmp.group(3).strip().decode('iso-8859-2'), tmp.group(4).strip().decode('iso-8859-2')  ))
            except IndexError:
                info.append( "Error")
            except AttributeError:
                pass

        return info
#<td class="bl_body" align="right"><img src="/_d/forum/a_lns.gif" align="absmiddle">&nbsp;&nbsp;1&nbsp;&nbsp;<a set="yes" linkindex="61" href="50937,345493989,1,200,200,74918615,345493989,49272,50,forum.html" class="ge_a">2</a>&nbsp; z &nbsp;<a set="yes" linkindex="62" href="50937,345493989,1,200,200,74918615,345493989,49272,50,forum.html" class="ge_a">2</a>&nbsp;<a set="yes" linkindex="63" href="50937,345493989,1,200,200,74918615,345493989,49272,50,forum.html"><img src="/_d/forum/a_pa.gif" align="absmiddle" border="0"></a></td>

    def get_comment_from_page(self, page):
        
        page_txt = self.get_html(page)
        pat3 = re.compile(r'<DIV CLASS=\"bl_body\"><A HREF=\"(.*?)\" CLASS=\"ge_a\">(.*?)</A></DIV>', re.DOTALL)
        c_list = pat3.findall(page_txt.decode('iso-8859-2'))
        return c_list

    def get_comment_list(self):

        c_list = []
        pat4 = re.compile(r'<td class=\"bl_body\" align=\"right\"><img src=\"/_d/forum/a_lns.gif\"(.*?)</td>',re.DOTALL)
        try:
            tmp = pat4.match(self.html).groups(1)
        except AttributeError:
            tmp = ("")

        pat5 = re.compile(r'href=\"(.*?)\".*?>(\d)</a>', re.DOTALL)
        c_pages = pat5.findall(tmp)

        to_search = set()
        to_search.add(self.address)
        

        if c_pages:
            for p in c_pages:
                to_search.add(self.base + p[0])

          

        for c in to_search:
            c_list.append(self.get_comment_from_page(c))


        return c_list

    def get_one_comment(self, comment_info):
        link, start = comment_info;
        start = start[:-6] #pomijamy ...\n
        start = start.encode('ISO-8859-2')
        link = self.base + link
        

        htm = self.get_html(link)
        #wybieranie komentarza
        pat6 = re.compile('<TD>'+re.escape(start)+'(.*?)</TD>')
        try:
          comment = start + pat6.findall(htm)[0]
        except:
          comment = "undefined -try watch on teh web"
        #wybieranie autora i daty
        pat7 = re.compile('<TD><br>(.*?), (.*?)</TD>')
        tmp = pat7.search(htm)
        try:
            author = tmp.group(1)
            date = tmp.group(2)
        except (IndexError, AttributeError):
            author, date = ("", "")


        return (comment.decode('iso-8859-2'), author.decode('iso-8859-2'), date)


#http://slowianska-twarz.blog.onet.pl/4559695,349812517,1,200,200,75611828,349822074,5937076,0,forum.html


